package com.mark.weka;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

import weka.clusterers.XMeans;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class WekaCluster {
	
	private ArffLoader loader;
	private Instances dataSet;
	private weka.clusterers.XMeans cluster;
	private int numOfClusters;
	private String newAttribute;
	private File arffFile;
	private int sizeOfDataset;
	
	public WekaCluster(File arffFile) {
		this.arffFile = arffFile;
		doCluster();
	}
	
	private void doCluster() {
		loader = new ArffLoader();
		newAttribute = "";
		try{
			loader.setFile(arffFile);
			dataSet = loader.getDataSet();
			cluster = new XMeans();
			cluster.setMaxIterations(4);
			//cluster.R_LOW = 7;
			//cluster.R_HIGH = 10;
			//cluster.R_WIDTH = 4;
			cluster.buildClusterer(dataSet);			
			numOfClusters = cluster.numberOfClusters();
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < numOfClusters; i++) {
				sb.append("s" + (i + 1) + " ");
				//System.out.println(sb);
			}
			newAttribute = sb.toString().trim();
			sizeOfDataset = dataSet.numInstances();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void newArffWriter() {
		int lineNum = 0;
		try{
			Scanner input = new Scanner(arffFile);
			PrintWriter out = new PrintWriter("C:\\txt.log");
					//CfUtil.GetFileNameNoExtFromFileName(arffFile.getName())+ "_classification.arff");
			while (input.hasNext()) {
				String line = input.nextLine();
				if (line.startsWith("@relation")) {
					out.println("@relation" + line.substring(9) + "_classification");
					System.out.println("line.substring(9) "+line.substring(9));
				}else if( line.startsWith("@data") ){
					out.println("@attribute shape {" + newAttribute + "}");
					out.println("@data");
				}else if (line.startsWith("@attribute")) {
					out.println(line);
				}else if (line.isEmpty()) {
					out.println();
				}else {
					line += ",class" + (cluster.clusterInstance(dataSet.instance(lineNum)) + 1);
					out.println(line);
					lineNum++;
				}
			}
			out.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public int clusterNewInstance(weka.core.Instance instance) {
		int indexOfCluster = -1;
		try{
			indexOfCluster = cluster.clusterInstance(instance);
		}catch( Exception e){
			e.printStackTrace();
		}
		return indexOfCluster;
	}
	
	public double[] frequencyOfCluster() {
		int[] sum = new int[this.numOfClusters];
		try{
			for (int i = 0; i < this.sizeOfDataset; i++) {
				sum[cluster.clusterInstance(dataSet.instance(i))]++;
				if( cluster.clusterInstance(dataSet.instance(i)) == 0){
					System.out.println(dataSet.instance(i)+"|"+cluster.clusterInstance(dataSet.instance(i)));
				}
			}
		}catch( Exception e ){
			e.printStackTrace();
		}
		double[] fre = new double[sum.length];
		for (int i = 0; i < sum.length; i++) {
			 fre[i] = (double)sum[i] / (double)this.sizeOfDataset;
		}
		 return fre;
	}
	
	
}
